package jnpf.service.impl;

import jnpf.entity.Whu_knowledgebaseEntity;
import jnpf.mapper.Whu_knowledgebaseMapper;
import jnpf.service.Whu_knowledgebaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.util.RandomUtil;


import jnpf.model.whu_knowledgebase.Whu_knowledgebasePagination;
import java.util.stream.Collectors;





import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.text.SimpleDateFormat;
import jnpf.util.*;
import java.util.*;

/**
 *
 * whu_knowledgebase
 * 版本： V3.2.0
 * 版权： 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * 作者： JNPF开发平台组
 * 日期： 2025-06-10
 */
@Service

public class Whu_knowledgebaseServiceImpl extends ServiceImpl<Whu_knowledgebaseMapper, Whu_knowledgebaseEntity> implements Whu_knowledgebaseService {

    @Autowired
    private UserProvider userProvider;






    @Override
    public List<Whu_knowledgebaseEntity> getList(Whu_knowledgebasePagination whu_knowledgebasePagination){
        String userId=userProvider.get().getUserId();
        List<String> AllIdList =new ArrayList();
        int total=0;
                    QueryWrapper<Whu_knowledgebaseEntity> whu_knowledgebaseQueryWrapper=new QueryWrapper<>();
            int whu_knowledgebaseNum =0;
			if(StringUtil.isNotEmpty(whu_knowledgebasePagination.getTodate())){
            whu_knowledgebaseNum++;
            List<String> TodateList =  whu_knowledgebasePagination.getTodate();
            Long fir = Long.valueOf(TodateList.get(0));
            Long sec = Long.valueOf(TodateList.get(1));

                whu_knowledgebaseQueryWrapper.lambda().ge(Whu_knowledgebaseEntity::getTodate, new Date(fir))
            .le(Whu_knowledgebaseEntity::getTodate, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));

                    }

			if(StringUtil.isNotEmpty(whu_knowledgebasePagination.getQuestion())){
            whu_knowledgebaseNum++;
            whu_knowledgebaseQueryWrapper.lambda().like(Whu_knowledgebaseEntity::getQuestion,whu_knowledgebasePagination.getQuestion());
                    }

			if(StringUtil.isNotEmpty(whu_knowledgebasePagination.getCompany())){
            whu_knowledgebaseNum++;
                whu_knowledgebaseQueryWrapper.lambda().eq(Whu_knowledgebaseEntity::getCompany,whu_knowledgebasePagination.getCompany());
                    }

			if(StringUtil.isNotEmpty(whu_knowledgebasePagination.getField())){
            whu_knowledgebaseNum++;
                whu_knowledgebaseQueryWrapper.lambda().eq(Whu_knowledgebaseEntity::getField,whu_knowledgebasePagination.getField());
                    }

			if(StringUtil.isNotEmpty(whu_knowledgebasePagination.getType())){
            whu_knowledgebaseNum++;
                whu_knowledgebaseQueryWrapper.lambda().eq(Whu_knowledgebaseEntity::getType,whu_knowledgebasePagination.getType());
                    }

            whu_knowledgebaseQueryWrapper.lambda().eq(Whu_knowledgebaseEntity::getUserid,userId);

        if(AllIdList.size()>0){
            whu_knowledgebaseQueryWrapper.lambda().in(Whu_knowledgebaseEntity::getId, AllIdList);
        }
        //排序
        if(StringUtil.isEmpty(whu_knowledgebasePagination.getSidx())){
        whu_knowledgebaseQueryWrapper.lambda().orderByDesc(Whu_knowledgebaseEntity::getTodate);
        }else{
        whu_knowledgebaseQueryWrapper="asc".equals(whu_knowledgebasePagination.getSort().toLowerCase())?whu_knowledgebaseQueryWrapper.orderByAsc(whu_knowledgebasePagination.getSidx()):whu_knowledgebaseQueryWrapper.orderByDesc(whu_knowledgebasePagination.getSidx());
        }
        if((total>0 && AllIdList.size()>0) || total==0){
        Page<Whu_knowledgebaseEntity> page=new Page<>(whu_knowledgebasePagination.getCurrentPage(), whu_knowledgebasePagination.getPageSize());
        IPage<Whu_knowledgebaseEntity> userIPage=this.page(page, whu_knowledgebaseQueryWrapper);
        return whu_knowledgebasePagination.setData(userIPage.getRecords(),userIPage.getTotal());
        }else{
            List<Whu_knowledgebaseEntity> list = new ArrayList();
            return whu_knowledgebasePagination.setData(list, list.size());
        }
    }
    @Override
    public List<Whu_knowledgebaseEntity> getList2(Whu_knowledgebasePagination whu_knowledgebasePagination){
        String userId=userProvider.get().getUserId();
        List<String> AllIdList =new ArrayList();
        int total=0;
        QueryWrapper<Whu_knowledgebaseEntity> whu_knowledgebaseQueryWrapper=new QueryWrapper<>();
        int whu_knowledgebaseNum =0;
        if(StringUtil.isNotEmpty(whu_knowledgebasePagination.getTodate())){
            whu_knowledgebaseNum++;
            List<String> TodateList =  whu_knowledgebasePagination.getTodate();
            Long fir = Long.valueOf(TodateList.get(0));
            Long sec = Long.valueOf(TodateList.get(1));

            whu_knowledgebaseQueryWrapper.lambda().ge(Whu_knowledgebaseEntity::getTodate, new Date(fir))
                    .le(Whu_knowledgebaseEntity::getTodate, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));

        }

        if(StringUtil.isNotEmpty(whu_knowledgebasePagination.getQuestion())){
            whu_knowledgebaseNum++;
            whu_knowledgebaseQueryWrapper.lambda().like(Whu_knowledgebaseEntity::getQuestion,whu_knowledgebasePagination.getQuestion());
        }

        if(StringUtil.isNotEmpty(whu_knowledgebasePagination.getCompany())){
            whu_knowledgebaseNum++;
            whu_knowledgebaseQueryWrapper.lambda().eq(Whu_knowledgebaseEntity::getCompany,whu_knowledgebasePagination.getCompany());
        }

        if(StringUtil.isNotEmpty(whu_knowledgebasePagination.getField())){
            whu_knowledgebaseNum++;
            whu_knowledgebaseQueryWrapper.lambda().eq(Whu_knowledgebaseEntity::getField,whu_knowledgebasePagination.getField());
        }

        if(StringUtil.isNotEmpty(whu_knowledgebasePagination.getType())){
            whu_knowledgebaseNum++;
            whu_knowledgebaseQueryWrapper.lambda().eq(Whu_knowledgebaseEntity::getType,whu_knowledgebasePagination.getType());
        }

        if(AllIdList.size()>0){
            whu_knowledgebaseQueryWrapper.lambda().in(Whu_knowledgebaseEntity::getId, AllIdList);
        }
        //排序
        if(StringUtil.isEmpty(whu_knowledgebasePagination.getSidx())){
            whu_knowledgebaseQueryWrapper.lambda().orderByDesc(Whu_knowledgebaseEntity::getTodate);
        }else{
            whu_knowledgebaseQueryWrapper="asc".equals(whu_knowledgebasePagination.getSort().toLowerCase())?whu_knowledgebaseQueryWrapper.orderByAsc(whu_knowledgebasePagination.getSidx()):whu_knowledgebaseQueryWrapper.orderByDesc(whu_knowledgebasePagination.getSidx());
        }
        if((total>0 && AllIdList.size()>0) || total==0){
            Page<Whu_knowledgebaseEntity> page=new Page<>(whu_knowledgebasePagination.getCurrentPage(), whu_knowledgebasePagination.getPageSize());
            IPage<Whu_knowledgebaseEntity> userIPage=this.page(page, whu_knowledgebaseQueryWrapper);
            return whu_knowledgebasePagination.setData(userIPage.getRecords(),userIPage.getTotal());
        }else{
            List<Whu_knowledgebaseEntity> list = new ArrayList();
            return whu_knowledgebasePagination.setData(list, list.size());
        }
    }
    @Override
    public List<Whu_knowledgebaseEntity> getTypeList(Whu_knowledgebasePagination whu_knowledgebasePagination,String dataType){
        String userId=userProvider.get().getUserId();
        List<String> AllIdList =new ArrayList();
		int total=0;
                    QueryWrapper<Whu_knowledgebaseEntity> whu_knowledgebaseQueryWrapper=new QueryWrapper<>();
            int whu_knowledgebaseNum =0;
			if(StringUtil.isNotEmpty(whu_knowledgebasePagination.getTodate())){
            whu_knowledgebaseNum++;
            List<String> TodateList =  whu_knowledgebasePagination.getTodate();
            Long fir = Long.valueOf(TodateList.get(0));
            Long sec = Long.valueOf(TodateList.get(1));

                whu_knowledgebaseQueryWrapper.lambda().ge(Whu_knowledgebaseEntity::getTodate, new Date(fir))
            .le(Whu_knowledgebaseEntity::getTodate, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));

                    }

			if(StringUtil.isNotEmpty(whu_knowledgebasePagination.getQuestion())){
            whu_knowledgebaseNum++;
            whu_knowledgebaseQueryWrapper.lambda().like(Whu_knowledgebaseEntity::getQuestion,whu_knowledgebasePagination.getQuestion());
                    }

			if(StringUtil.isNotEmpty(whu_knowledgebasePagination.getCompany())){
            whu_knowledgebaseNum++;
                whu_knowledgebaseQueryWrapper.lambda().eq(Whu_knowledgebaseEntity::getCompany,whu_knowledgebasePagination.getCompany());
                    }

			if(StringUtil.isNotEmpty(whu_knowledgebasePagination.getField())){
            whu_knowledgebaseNum++;
                whu_knowledgebaseQueryWrapper.lambda().eq(Whu_knowledgebaseEntity::getField,whu_knowledgebasePagination.getField());
                    }

			if(StringUtil.isNotEmpty(whu_knowledgebasePagination.getType())){
            whu_knowledgebaseNum++;
                whu_knowledgebaseQueryWrapper.lambda().eq(Whu_knowledgebaseEntity::getType,whu_knowledgebasePagination.getType());
                    }

		if(AllIdList.size()>0){
        whu_knowledgebaseQueryWrapper.lambda().in(Whu_knowledgebaseEntity::getId, AllIdList);
				}
        //排序
        if(StringUtil.isEmpty(whu_knowledgebasePagination.getSidx())){
        whu_knowledgebaseQueryWrapper.lambda().orderByDesc(Whu_knowledgebaseEntity::getTodate);
        }else{
        whu_knowledgebaseQueryWrapper="asc".equals(whu_knowledgebasePagination.getSort().toLowerCase())?whu_knowledgebaseQueryWrapper.orderByAsc(whu_knowledgebasePagination.getSidx()):whu_knowledgebaseQueryWrapper.orderByDesc(whu_knowledgebasePagination.getSidx());
        }
        if("0".equals(dataType)){
            if((total>0 && AllIdList.size()>0) || total==0){
                Page<Whu_knowledgebaseEntity> page=new Page<>(whu_knowledgebasePagination.getCurrentPage(), whu_knowledgebasePagination.getPageSize());
                IPage<Whu_knowledgebaseEntity> userIPage=this.page(page, whu_knowledgebaseQueryWrapper);
                return whu_knowledgebasePagination.setData(userIPage.getRecords(),userIPage.getTotal());
            }else{
                List<Whu_knowledgebaseEntity> list = new ArrayList();
                return whu_knowledgebasePagination.setData(list, list.size());
            }
        }else{
            return this.list(whu_knowledgebaseQueryWrapper);
        }
    }

    @Override
    public Whu_knowledgebaseEntity getInfo(String id){
        QueryWrapper<Whu_knowledgebaseEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(Whu_knowledgebaseEntity::getId,id);
        return this.getOne(queryWrapper);
    }

    @Override
    public void create(Whu_knowledgebaseEntity entity){
        this.save(entity);
    }

    @Override
    public boolean update(String id, Whu_knowledgebaseEntity entity){
        entity.setId(id);
        return this.updateById(entity);
    }
    @Override
    public void delete(Whu_knowledgebaseEntity entity){
        if(entity!=null){
            this.removeById(entity.getId());
        }
    }
    //子表方法

    //列表子表数据方法

}
